from appium import webdriver
from appium.webdriver.webdriver import WebDriver


class BasePage:

    def __init__(self, driver: WebDriver = None):
        self.driver = driver

    def find_by_element(self, by, value):
        return self.driver.find_element(by, value)

    def finds(self, by, value):
        return self.driver.find_elements(by, value)

    def find_and_click(self, by, value):
        self.find_by_element(by, value).click()

    def find_and_sendkeys(self, by, value, content):
        self.find_by_element(by, value).send_keys(content)


    # 获取页面大小
    def slip_elements(self):
        # 获取窗口大小
        window_size = self.driver.get_window_size()
        self.driver.implicitly_wait(2)
        width = window_size.get("width")
        height = window_size.get('height')
        startx = width / 2
        starty = height * 0.8
        endy = height * 0.2
        duration = 2000
        self.driver.swipe(start_x=startx, start_y=starty, end_x=startx, end_y=endy,duration=duration)

    def set_implicitly_wait(self, time=1):
        """
        设置隐式等待
        :param time: 隐式等待时间
        :return:
        """
        self.driver.implicitly_wait(time)

    # 定义一个滑动方法
    def slip_find_elements(self, by, value):
        while True:
            eles = self.finds(by, value)
            if(len(eles)) >= 1:
                return eles[0]
            else:
                self.set_implicitly_wait()
                self.slip_elements()

